<?php
defined('SYSTEM_PATH') or die('Han che truy cap');


class Uri extends Object
{
    private $controller = NULL;
    private $method = NULL;
    private $params = array();
    
    //Request type :
    // 0 : default controller and default method are called
    // 1 : controller specified. Default method are called
    // 2 : controller and method are specified (without params)
    // 3 : controller and method are specified with params
    private $requestType = 1;
    
    
    function __construct()
    {
        $this->urlAnalyze();
    }
    
    public function urlAnalyze()
    {
        if(!isset($_SERVER['PATH_INFO']))
        {
            $this->requestType = 0;
            return;
        }
        $arr = explode('/', $_SERVER['PATH_INFO']);                
        if($arr[1] != NULL) //indicate that a controller is called
        {
            $this->controller = $arr[1];
            if(sizeof($arr) > 2 && $arr[2] != NULL)
            {
                $this->method = $arr[2];
                $this->requestType = 2;
            }
            else
            {
                $this->requestType = 1;
                return;
            }
            
            $url['params'] = array();
            if(sizeof($arr) > 3)
            {
                $this->requestType = 3;
                for($i = 3;$i < sizeof($arr); $i++)
                $this->params[] = $arr[$i];
            }
        }
        else
        {
            $this->requestType = 0;
        }
    }
    
    /**
     *
     * @return type 
     */
    public function getController()
    {
        return $this->controller;
    }
    
    /**
     *
     * @param type $name 
     */
    public function setController($name)
    {
        $this->controller = $name;
    }
    
    /**
     *
     * @return type 
     */
    public function getMethod()
    {
        return $this->method;
    }
    
    /**
     *
     * @param type $name 
     */
    public function setMethod($name)
    {
        $this->method = $name;
    }
    
    /**
     *
     * @return type 
     */
    public function getRequestType()
    {
        return $this->requestType;
    }
    
    /**
     *
     * @return type 
     */
    public function getParams()
    {
        return $this->params;
    }
    
    /**
     * @param array Array of function
     */
    public function setParams()
    {
        $array = func_get_args();
        foreach ($array as $value)
        {
            $this->params[] = $value;
        }
    }        
    

}

?>
